package com.iisigroup.project.statistics.caliberRM;

import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils;

import com.starbase.caliber.*;
import com.starbase.caliber.attribute.Attribute;
import com.starbase.caliber.server.*;

public class ConnectionCreator {
    
    /**
     * @return
     * @throws NoAvailableLicenseException
     * @throws InvalidLoginException
     * @throws AccountDisabledException
     * @throws UserMustChangePasswordException
     * @throws LicenseExpirationException
     * @throws RemoteServerException
     */
    public static Session createSession() throws NoAvailableLicenseException, InvalidLoginException, AccountDisabledException, UserMustChangePasswordException,
	    LicenseExpirationException, RemoteServerException {
	final CaliberServer server = new CaliberServer("192.168.10.8");
	Session session = null;
	session = server.login("admin", "admin");
	return session;	
    }

    
    /**
     * 根據專案名稱,系統名稱
     * @param sampleProjectName 專案名稱
     * @param sysmtemName  系統名稱
     * @param session
     * @return
     * @throws RemoteServerException
     */
    public static TreeNode retrieveFromProject(final String sampleProjectName, final String sysmtemName, final Session session) throws RemoteServerException {
	TreeNode result = null;
	if (session != null) {
	    Project[] arrayOfProjects = session.getProjects();
	    outter: for (int i = 0; i < arrayOfProjects.length; i++) {
		final Project project = arrayOfProjects[i];
		final String projectName = project.getName();
		if (StringUtils.equals(sampleProjectName, projectName)) {
		    final Baseline currentBaseline = project.getCurrentBaseline();
		    // Get current baseline's tree.
		    RequirementTree tree = (RequirementTree) currentBaseline.getRequirementTree();
		    // Get the tree's root node.
		    RequirementTreeNode root = (RequirementTreeNode) tree.getRoot();
		    TreeNode[] children = root.getChildren();
		    for (TreeNode node : children) {
			if (sysmtemName.equals(node.getName())) {
			    result = node;
			    break outter;
			}
		    }
		}
	    }
	}
	return result;
    }
    public static Project retrieveProject(final String sampleProjectName,  final Session session) throws RemoteServerException {
	Project result = null;
	if (session != null) {
	    Project[] arrayOfProjects = session.getProjects();
	    outter: for (int i = 0; i < arrayOfProjects.length; i++) {
		final Project project = arrayOfProjects[i];
		final String projectName = project.getName();
		if (StringUtils.equals(sampleProjectName, projectName)) {
		    result = project;
		    break;
		}
	    }
	}
	return result;
    }
    
    public static void test01(){
	CaliberServer server = new CaliberServer("192.168.10.8");
	try {
	    Session session = server.login("admin", "admin");
	    if (session != null) {
		System.out.println(session);
				
		
		
		Project[] arrayOfProjects = session.getProjects();
		for (int i = 0; i < arrayOfProjects.length; i++) {
		    final Project project = arrayOfProjects[i];
		    System.out.println("project: " + project.getName());
		    if (StringUtils.equals("SRIS", project.getName())) {
			final RequirementType[] requirementTypes = project.getRequirementTypes();
			for (RequirementType rt : requirementTypes) {
			    String requirementTypeName = rt.getName();
			    System.out.println(requirementTypeName);
			    if (StringUtils.equals("RL", requirementTypeName)) {
				System.out.println(rt.getClass().getCanonicalName());
				System.out.println("Caliber Object id: "+rt.getID());
				
				final	Attribute[] attributes = rt.getAttributes();
				for(Attribute attr : attributes){
				    System.out.println("attr: "+attr.getName());
				}
				
				if( ClassUtils.isAssignable(rt.getClass(), TreeNode.class) ){
				    System.out.println("yes");
				    
				}
			    }
			}
			
			final Baseline currentBaseLine = project.getCurrentBaseline();
			
			//使用currentBaseLine...將tree顯示出來
			displayRequirementTree(currentBaseLine);
		    }
		}
		// final User[] users = session.getUsers();
		// for(User user : users){
		// System.out.println("user: "+user.getName());
		//
		// }
	    }
	} catch (RemoteServerException rse) {
	    rse.printStackTrace();
	}
    } 

   
    public static void displayRequirementTree(final Baseline currentBaseline) throws RemoteServerException {
	// Get current baseline's tree.
	RequirementTree tree = (RequirementTree) currentBaseline.getRequirementTree();
	// Get the tree's root node.
	RequirementTreeNode root = (RequirementTreeNode) tree.getRoot();
	TraverseTree(root);

    }
   

    public static void TraverseTree(TreeNode parentNode) {
	// Print relevant information about the current node.
	PrintTreeNode(parentNode);
	// Iterate through the parent's collection of children...
	TreeNode[] children = parentNode.getChildren();
	for (int i = 0; i < children.length; i++) {
	    TraverseTree(children[i]);
	}
    }

    private static void PrintTreeNode(TreeNode node) {
	StringBuffer sb = new StringBuffer();
	for (int i = 0; i < node.getLevel(); i++) {
	    sb.append(" ");
	}
	sb.append(node.getHierarchyNumber());
	sb.append(" ");
	sb.append(node.getName());
	sb.append(" ");
	sb.append(node.getSerialNumberTag());
	sb.append("  ");
	sb.append(node.getClass().getSimpleName());
	sb.append("  ");
	sb.append(node.getHierarchyNumber());
	System.out.println(sb);
    }

}
